import {getAccessToken} from '@/utils/auth'
import store from "@/store"

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
    '/pages/login',
    '/pages/mine/forget-pwd/forget-pwd',
    // '/pages/common/webview/index',
    // '/pages/devices/index',
    // '/pages/police/index',
    // '/pages/police/police-deal/police-deal',
    // '/pages/police/police-res/police-res',
    // '/pages/police/police-record/police-record',
    // '/pages/police/police-detail/police-detail',
    // '/pages/devices/detail/index',
    // '/pages/devices/detailparam/index',
    '/pages/home/index',
    '/pages/mine/index',
    // '/pages/mine/peosoninfo/peosoninfo',
    // '/pages/mine/accept-police-list/accept-police-list',
    // '/pages/mine/add-police/add-police',
    // '/pages/mine/tariff-manage/tariff-manage',
    // '/pages/mine/order-record/order-record',
    // '/pages/mine/phone-sms-recharge/phone-sms-recharge',
    // '/pages/mine/setting/index',
    // '/pages/mine/change-pwd/change-pwd',
    // '/pages/mine/delete-account/delete-account',
    // '/pages/mine/change-mobile/change-mobile',
    // '/pages/devices/detector-details/detector-details',
    // '/pages/devices/detector/detector',
	// '/pages/common/choose-img/choose-img'
]

// 检查地址白名单
function checkWhite(url) {
    const path = url.split('?')[0]
    return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
    uni.addInterceptor(item, {
        invoke(to) {
            if (getAccessToken()) {
                if (to.path === loginPage) {
                    uni.reLaunch({url: "/"})
                }
                // console.log('store.getters.isSetDict >>',store.getters.isSetDict )
                // console.log('store.getters.dictMap >>',store.getters.dictMap)

                // if(!store.getters.isSetDict) {
                //   store.dispatch('GetDictMap')
                // }
                return true
            } else {
                if (checkWhite(to.url)) {
                    return true
                }
                uni.reLaunch({url: loginPage})
                return false
            }
        },
        fail(err) {
            console.log(err)
        }
    })
})